package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.keys.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserRedisService redisService;



    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        AssertUtil.hasLength(phone,"手机号不能为空!");
        if(redisService.getVerifyCode(phone) != null){
            throw new LogicException("操作过于频繁, 请稍后重试");
        }
        String code = UUID.randomUUID().toString().substring(0,4);
        StringBuilder text = new StringBuilder(100);
        text.append("短信验证码为:").append(phone).append("请在5分钟内使用.");
        redisService.setVerifyCode(phone,code);
        System.out.println("Verify_Code:" + phone + "->" + code);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String code) {
        AssertUtil.hasLength(phone,"电话号码不能为空!");
        AssertUtil.hasLength(nickname,"昵称不能为空!");
        AssertUtil.hasLength(password,"密码不能为空!");
        AssertUtil.hasLength(code,"验证码不能为空!");
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不一致!");
        if(!AssertUtil.isPhone(phone)){
            throw new LogicException("电话号码格式不正确!");
        }
        String productCode = redisService.getVerifyCode(phone);
        if(productCode == null){
            throw new LogicException("验证码已失效,请重新获取!");
        }
        AssertUtil.isEquals(productCode.toLowerCase(),code.toLowerCase(),"验证码错误!");
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPassword(AssertUtil.getMd5(password));
        userInfo.setPhone(phone);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) {
        UserInfo userInfo = userInfoMapper.selectByPhoneAndPassword(username,AssertUtil.getMd5(password));
        if (userInfo == null){
            throw new LogicException("用户名或密码错误!");
        }
        String key = UUID.randomUUID().toString().replace("-","");
        redisService.setUserInfo(key,userInfo);
        return key;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(UserInfo userInfo) {

        userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public void updateHeadImg(String headImgUrl, Long uid) {
        userInfoMapper.updateHeadImg(headImgUrl,uid);
    }

    @Override
    public void updatePassword(String rpassword, Long uid) {
        userInfoMapper.updatePassword(AssertUtil.getMd5(rpassword),uid);
    }

    @Override
    public boolean changePhone(String newMobile, String phone, String verifyCode) {

        if (newMobile != null){
            if (verifyCode != null){
                //判断验证码是不是相等
                String code = redisService.getVerifyCode(newMobile);
                if (verifyCode.equalsIgnoreCase(code)) {
                    userInfoMapper.updatePhone(newMobile,phone);
                    return true;
                }
            }
        }
        return false;
    }
}
